import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:get/get.dart';

class UtilsDialog {
  //dialog的底部构建
  static Future<T?> _abstractDialog<T>(BuildContext context,
      {bool barrierDismissible = true, WidgetBuilder? builder}) {
    return showDialog<T?>(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (context) {
          return MediaQuery(
            //不受系统字体缩放影响
            data: MediaQueryData.fromView(View.of(context))
                .copyWith(textScaleFactor: 1),
            child: SafeArea(
              child: builder!(context),
            ),
          );
        });
  }

  static Future<void> showLoadingDialog(BuildContext context, {String content = "loading"}) {
    return _abstractDialog(context, builder: (BuildContext context) {
      return Material(
        color: Colors.transparent,
        child: WillPopScope(
          onWillPop: () => Future.value(false),
          child: Center(
            child: Container(
              width: 200.0,
              height: 200.0,
              padding: const EdgeInsets.all(4.0),
              decoration: const BoxDecoration(
                //背景装饰
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(4.0)),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  const Padding(
                    padding: EdgeInsets.only(bottom: 5.0),
                    child: SpinKitWanderingCubes(
                      color: Colors.cyan,
                    ),
                  ),
                  Text(content.tr)
                ],
              ),
            ),
          ),
        ),
      );
    });
  }

  static showSingleNoticeDialog(BuildContext context, String message, {VoidCallback? onPressed}) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            content: Text(message),
            actions: <Widget>[
              SizedBox(
                width: 60,
                height: 40,
                child: TextButton(
                    onPressed: onPressed, child: Text('ok'.tr),),
              )
            ],
          );
        });
  }

  static showNoticeDialog(BuildContext context, String message, {VoidCallback? onOk,VoidCallback? onCancel}) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            content: Text(message),
            actions: <Widget>[
              SizedBox(
                width: 60,
                height: 40,
                child: TextButton(
                  onPressed: onOk, child: Text('确认'.tr),),
              ),
              SizedBox(
                width: 60,
                height: 40,
                child: TextButton(
                  onPressed: onCancel, child: Text('取消'.tr),),
              )
            ],
          );
        });
  }
}
